草庐IT

PHP unicode解码

全部标签

json - 解码未知格式的 JSON 数据

这个问题在这里已经有了答案:UnmarshalJSONwithsomeknown,andsomeunknownfieldnames(8个答案)关闭3年前。我的JSON格式如下:{'Math':[{'Student1':100.0,'timestamp':Timestamp('2017-06-2615:30:00'),'Student2':100.0,'Student3':97.058823442402414},{'Student1':93.877550824911907,'timestamp':Timestamp('2017-06-2615:31:00'),'Student2':100

json - 如何将 JSON 文档中的字符串值解码为 int 类型的变量?

我的结构:typeUserstruct{FirstNamestring`json:"firstname,omitempty"validate:"required"`LastNamestring`json:"lastname,omitempty"validate:"required"`NumberofDaysint`json:"numberofdays,string"validate:"min=0,max=100"`}NumberofDays的值作为字符串从服务器传递,但我想检查它是否在范围内并存储为int。例如:user:=&User{"Michael","Msk","3"}我收到“无

go - 创建一个函数来测试从接口(interface)编码/解码

我想创建一个简单的函数来测试编码/解码记录是否按预期工作。我只是在这个例子中使用JSON:packagetestimport("encoding/json""fmt""testing""reflect""github.com/stretchr/testify/require")funcCheckRoundTripJSON(t*testing.T,recordinterface{}){data,err:=json.Marshal(record)require.NoError(t,err)fmt.Println("Record:",record,"wasencodedto:",data,"

go - 有没有办法根据内容动态解码 json?

这个问题在这里已经有了答案:HowtoparseJSONingolangwithoutunmarshalingtwice(3个答案)HowtoparseacomplicatedJSONwithGounmarshal?(3个答案)DecodinggenericJSONobjectstooneofmanyformats(1个回答)HowtopartiallyparseJSONusingGo?(3个答案)关闭3年前。我有一个像这样的json格式{"my_object_list":[{"meta":{"version":1},"my_value":{//Somecomplexvalue}}{"

json - Golang json.decoder 无法仅解码来自浏览器的请求

我的golang应用无法解码来自浏览器的表单,但在使用curl和httpie时成功。给定这段代码:typeMemberstruct{Usernamestring`json:"username"`Emailstring`json:"email"`Passwordstring`json:"password"`}funcRegister(whttp.ResponseWriter,r*http.Request,phttprouter.Params){vartMemberjson.NewDecoder(r.Body).Decode(&t)log.Println(t.Username)log.Pr

json - Golang 结构字段名称并解码到此结构中

我写了一个示例程序来说明我的问题,可以在这里查看:https://play.golang.org/p/6776lYcbBR所以我的问题是:当结构(GameOne)字段的名称以大写字母开头时,json.Unmarshal用作预期的;当它以小写字母(GameTwo)开头时,字段值设置为默认值。为什么会这样?与范围/可见性规则有关吗?提前谢谢你。 最佳答案 json.Unmarshal仅设置结构中的导出字段,并且对于导出字段,首字母必须大写。有关更多信息,我强烈建议您查看documentation

json - 在 go 中解码 JSON 结构制作空 map

这个问题在这里已经有了答案:PrintingEmptyJsonasaresult[duplicate](1个回答)关闭9个月前。我有一个json文件,其中包含大量数据:{"elec":{"s20":{"coldS":{"wDay":{"Night":{"avg":1234,"stddev":56},"Morning":{"avg":5432,"stddev":10}...},...},...},...}我想将这个文件加载为一个go结构:typeConsumConfigstruct{elecmap[string]map[string]map[string]map[string]Consu

json - 如何解码包含整数、 float 和数字字符串的 JSON?

我有多个不同的JSON数据请求被传递到我的Go应用程序,其中包含不同格式的数字。请求示例如下:{"stringData":"123456","intData":123456,"floatData":123456.0}有没有办法将此数据解码为由JSON数据确定的类型。例如,字符串数据为“123456”,整型数据为123456,浮点型数据为123456.0。我没有为这些JSON对象定义结构,因此无法为这些对象创建结构。我看过decoder.UseNumber()方法将数据转换成字符串,但我不知道之后如何处理stringData和intData之间的差异。 最佳答

go - 如何按照 api 指南将 json 解码为 golang 中的结构

这是我的body/api如何发布数据:{"data":{"email":"string","first_name":"string","last_name":"string",}}这是我的postProfileRequest结构,也许我需要更改它以容纳数据?typepostProfileRequeststruct{ProfileProfile}这里是个人资料typeProfilestruct{IDint`json:"id"`Emailstring`json:"email"`FirstNamestring`json:"first_name"`LastNamestring`json:"la

xml - 从 xml.Marshal() 的结果中解码 XML

我有一个小的struct,我想使用encoding/xml对它进行编码和解码。包裹:typePointstruct{X,Yintzint//unexportedNames[]string}当我使用encoding/json时编码/解码工作正常包。但是当我使用encoding/xml包时,只有xml.Marshal()有效,xml.Unmarshal()返回一个错误:invalidcharacter'我是这样处理XML的:p:=Point{1,2,3,[]string{"Bob","Alice"}}data,err:=xml.Marshal(p)iferr!=nil{fmt.Printl